package com.hycc.base;

public class TrafficLight {
    public static void main(String[] args) {
        Sketch sketch = new Sketch();
        new Painter(sketch).start();
        new Model(sketch).start();
    }
}

//生产者--画家
class Painter extends Thread{
    Sketch opus;

    public Painter(Sketch opus) {
        this.opus = opus;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            this.opus.draw("SketchId" + i);
        }
    }
}

//消费者--模特
class Model extends Thread{

    Sketch opus;

    public Model(Sketch opus) {
        this.opus = opus;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            this.opus.watch();
        }
    }
}

//产品--素描画
class Sketch{
    //画家素描，模特等待 T
    //模特观看，画家等待 F
    String opus; //作品
    boolean flag = true;

    public synchronized void draw(String oups){
        if(!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("画家完成了素描"+oups);
        this.opus = oups;
        this.flag=!this.flag;
        //通知模特观看
        this.notifyAll();
    }

    public synchronized void watch(){
        if(flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("模特观看了作品"+opus);
        this.flag=!this.flag;
        //通知画家创作
        this.notifyAll();
    }
}